<?php

namespace app\model\v1\recruit;

use app\Request;
use crmeb\traits\ModelTrait;
use think\Model;

class RecruitIndustry extends Model
{


    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
     //   'create_time','update_time','delete_time'
    ];


    public static function getIndustryData($id = 0)
    {
        $result = [];
        $item = self::whereNull("delete_at")->where('pid',$id)->select()->toArray();
        if ($item){
            foreach($item as $k=> $v) {
                $item[$k]['children'] = [];
                $data = self::getIndustryData($v['id']);
                if ($data){
                    foreach($data as $kk=> $vs) {
                        $item[$k]['children'][$kk] = $vs;
                    }
                }
                $result[$k] =$item[$k];
            }
        }
        return $result;
    }


    /**
     * 求职招聘后台 - 行业管理列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getRecruitIndustryList($param = [])
    {
        $data["data"] = self::field ("*")
            ->whereNull("delete_at")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("industry_name",'like', '%'.$param["word"].'%');
                if(!empty($param["pid"])) $query->where("pid",  $param["pid"]);
            })
            ->order("id", "desc")
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->whereNull("delete_at")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("industry_name",'like', '%'.$param["word"].'%');
                if(!empty($param["pid"])) $query->where("pid",  $param["pid"]);
            })
            ->count();

        if(!empty($data["data"])){
            foreach ($data["data"] as $key => &$val){
                $val["pid_name"] = self::getPidName($val["pid"]);
            }
        }

        return $data;
    }


    public static function getPidName($pid)
    {
        $arr = [];
        $parent = self::where("id", $pid)->find();
        if(!empty($parent)){
            $arr[] = $parent['industry_name'] ?? '';
            if($parent["pid"] > 0){
                $parent2 = self::where("id", $parent["pid"])->find();
                if(!empty($parent2)){
                    $arr[] = $parent2['industry_name'] ?? '';
                    $parent3 = self::where("id", $parent2["pid"])->find();
                    if(!empty($parent3)){
                        $arr[] = $parent3['industry_name'] ?? '';
                    }
                }
            }
        }
        return !empty($arr) ? implode("/", array_reverse($arr)) : '';
    }





    /**
     * 行业管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addRecruitIndustry($param = [])
    {
        $add = [
            "pid"               => $param["pid"],
            "industry_name"     => $param["industry_name"] ?? '',
        ];
        return self::insert($add);
    }


    /**
     * 行业管理列表 - 编辑
     * @param $param
     * @return RecruitIndustry
     */
    public static function updateRecruitIndustry($param = [])
    {
        $update = [];
        if(!empty($param["industry_name"])) $update["industry_name"] = $param["industry_name"];
        if(!empty($param["pid"])) $update["pid"] = $param["pid"];

        return self::where("id", $param["id"])->update($update);
    }





    public static function enableRecruitIndustry($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }






}